<?php
namespace Cms\Controller;
use Cms\Controller\BaseController;
class GameController extends BaseController {
	
	public function _initialize() {
		//你可以在此覆盖父类方法	
		parent::_initialize();
		$this->assign('useroath',$_SESSION['CMS']['user']['oath']);
	}
	
	
	public function set() {
		$m = M('vip_set');
		$data = $m->find();
		if (IS_POST) {
			$post = I('post.');
			if($post['isgift']==1){
				$post['gift_detail']=$post['gift_type'].",".$post['gift_money'].",".$post['gift_days'].",".$post['gift_usemoney'];
			}
			unset($post['gift_type']);
			unset($post['gift_money']);
			unset($post['gift_days']);
			unset($post['gift_usemoney']);
			$r = $data ? $m->where('id='.$data['id'])->save($post) : $m->add($post);
			if (FALSE!==$r) {
				$info['status']=1;
				$info['msg']='设置成功！';
			} else {
				$info['status']=0;
				$info['msg']='设置失败！';
			}
			$this->ajaxReturn($info,"json");
		} else {
			//设置面包导航，主加载器请配置
			$bread=array(
				'0'=>array(
					'name'=>'会员中心',
					'url'=>U('Cms/Vip/#')
				),
				'1'=>array(
					'name'=>'会员设置',
					'url'=>U('Cms/Vip/set')
				)
			);
			$this->assign('breadhtml',$this->getBread($bread));
			$data = $m->find();
			if($data['isgift']==1) {
				$gift=explode(",", $data['gift_detail']);
				$data['gift_type']=$gift[0];
				$data['gift_money']=$gift[1];
				$data['gift_days']=$gift[2];
				$data['gift_usemoney']=$gift[3];
			}
			$this->assign('data',$data);
			$this->display();
		}
	}
	
	//CMS后台会员等级列表
	public function level(){
		//设置面包导航，主加载器请配置
		$bread=array(
			'0'=>array(
				'name'=>'游戏中心',
				'url'=>U('Cms/Game/#')
			),
			'1'=>array(
				'name'=>'等级称号',
				'url'=>U('Cms/Game/level')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		//绑定搜索条件与分页
		$m=M('Game_level');
		$p=$_GET['p']?$_GET['p']:1;
		$name=I('name')?I('name'):'';
		if($name){
			$map['name']=array('like',"%$name%");
			$this->assign('name',$name);
		}
		$psize=self::$CMS['set']['pagesize']?self::$CMS['set']['pagesize']:20;
		$cache=$m->where($map)->order('exp')->page($p,$psize)->select();
		foreach($cache as $k=>$v){
			$m=M('Upload_img');
			$map['id']=array('in',$v['medal']);
			$c=$m->where($map)->find();
			$cache[$k]['img'] =$c['savepath'].$c['savename'];
		}
		$count=$m->where($map)->count();
		$this->getPage($count, $psize, 'Joel-loader', '分组列表','Joel-search');
		$this->assign('cache',$cache);		
		$this->display();
	}
	
	//CMS后台会员等级设置
	public function levelSet(){
		$id=I('id');
		$m=M('Game_level');
		//设置面包导航，主加载器请配置		
		$bread=array(
			'0'=>array(
				'name'=>'游戏中心',
				'url'=>U('Cms/Game/#')
			),
			'1'=>array(
				'name'=>'等级称号',
				'url'=>U('Cms/Game/level')
			),
			'2'=>array(
				'name'=>'称号设置',
				'url'=>$id?U('Cms/Game/levelSet',array('id'=>$id)):U('Cms/Game/levelSet')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		//处理POST提交	
		if(IS_POST){
			$data=I('post.');
			$re=$id?$m->save($data):$m->add($data);
			if(FALSE!==$re){
				$info['status']=1;
				$info['msg']='设置成功！';
			}else{
				$info['status']=0;
				$info['msg']='设置失败！';
			}
			$this->ajaxReturn($info);
		} else {
			if($id){			
				$cache=$m->where('id='.$id)->find();
				$this->assign('cache',$cache);	
			}	
			$this->display();
		}
	}
	
	public function levelDel(){
		$id=$_GET['id'];//必须使用get方法
		$m=M('Game_level');
		if(!id){
			$info['status']=0;
			$info['msg']='ID不能为空!';
			$this->ajaxReturn($info);
		}
		$re=$m->delete($id);
		if($re){
			$info['status']=1;
			$info['msg']='删除成功!';
		}else{
			$info['status']=0;
			$info['msg']='删除失败!';
		}
		$this->ajaxReturn($info);
	}
	
    //CMS后台  战绩列表
	public function score(){
		//设置面包导航，主加载器请配置
		$bread=array(
			'0'=>array(
				'name'=>'游戏中心',
				'url'=>U('Cms/Game/#')
			),
			'1'=>array(
				'name'=>'战绩记录',
				'url'=>U('Cms/Game/score')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		//绑定搜索条件与分页
		$m= new \Cms\Model\TUsersModel();
		$p=$_GET['p']?$_GET['p']:1;
		$account=I('account')?I('account'):'';
		if($account){
			$map['account']=array('like',"%$account%");
			$this->assign('account',$account);
		}
		//$map['uid']=self::$UID;
		$psize=self::$CMS['set']['pagesize']?self::$CMS['set']['pagesize']:20;
		$cate0=$m->where($map)->page($p,$psize)->select();

		$count=$m->where($map)->count();
		$this->getPage($count, $psize, 'Joel-loader', '战绩记录','Joel-search');
		$this->assign('cache',$cate0);		
		$this->display();
	}
	
	//游戏战绩列表设置
	public function scoreSet(){
		$m= new \Cms\Model\TUsersModel();
		$id=I('userid');
		//设置面包导航，主加载器请配置		
		$bread=array(
			'0'=>array(
				'name'=>'游戏中心',
				'url'=>U('Cms/Game/index')
			),
			'1'=>array(
				'name'=>'战绩记录',
				'url'=>U('Cms/Game/score')
			),
			'2'=>array(
				'name'=>'战绩记录 编辑',
				'url'=>U('Cms/Game/scoreSet')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		//处理POST提交	
		if(IS_POST){
			$data=I('post.');

			//追入操作员日志
			$old=$m->where('userid='.$id)->find();
			if(!$old){
				$info['status']=1;
				$info['msg']='未取得原始会员信息！';
				$this->ajaxReturn($info);
			}
			$adlog['uid']=$_SESSION['CMS']['uid'];
			$adlog['admin']=$_SESSION['CMS']['user']['username'];
			$adlog['vipid']=$old['userid'];
			$adlog['vip']=$old['account'];
			$adlog['ip']=get_client_ip();
			$adlog['ctime']=time();
			$adlog['coins']=$old['coins'].' => '.$data['coins'];
			$adlog['lv']=$old['lv'].' => '.$data['lv'];
			if($old['coins']<>$data['coins']){
				$adlog['event']='金币变动。';
			};
			if($old['lv']<>$data['lv']){
					$adlog['event']=$adlog['event'].'等级变动。';
			};
			if(($old['coins']==$data['coins'])&&($old['lv']==$data['lv'])){
					$adlog['event']='数据未改动！';
			}
			$radlog=M('Adminlog_score')->add($adlog);			
			if($id){
				$re=$m->save($data);
				if(FALSE!==$re){
					$info['status']=1;
					$info['msg']='设置成功！';
				}else{
					$info['status']=0;
					$info['msg']='设置失败！';
				}			
			}
			
			$this->ajaxReturn($info);
		}
		//处理编辑界面
		if($id){			
			$cache=$m->where('userid='.$id)->find();
		}

		$this->assign('cache',$cache);	
		$this->display();
	}
	
	//活动列表
	public function scoreDel(){
		$userid=$_GET['userid'];//必须使用get方法
		$m= new \Cms\Model\TUsersModel();
//		if(!$id){
//			$info['status']=0;
//			$info['msg']='ID不能为空!';
//			$this->ajaxReturn($info);
//		}
		if(!$userid){
			$data['lv']=0;
			$data['coins']=0;	
			$re=$m->where('1=1')->save($data);
		}else{
			$data['lv']=0;
			$data['coins']=0;
			$a = explode(",", $userid);
			if(count($a)>1)
				$userid = substr_replace($userid, '', -1, 1);
			$re=$m->where('userid in ('.$userid.')')->save($data);
		}
			//追入操作员日志
			$adlog['uid']=$_SESSION['CMS']['uid'];
			$adlog['admin']=$_SESSION['CMS']['user']['username'];
			$adlog['vipid']=(!$userid)?'':$userid;
			$adlog['vip']=(!$userid)?'':$userid;
			$adlog['ip']=get_client_ip();
			$adlog['ctime']=time();
			$adlog['coins'] = ' => 0';
			$adlog['lv'] = ' => 0';
			$adlog['event']='金币清空。';
			$adlog['event']=$adlog['event'].'等级清空。';
			$radlog=M('Adminlog_score')->add($adlog);	
					
		//echo $m->getLastSql();die;
		if($re){
			$info['status']=1;
			$info['msg']='清空成功!';
		}else{
			$info['status']=0;
			$info['msg']='清空失败!';
		}
		$this->ajaxReturn($info);
	}
	
}